import React, {Component} from 'react';
import {Button, Space, Select} from 'antd';
//用户获取store中管理的状态
const {Option} = Select;

class Count extends Component {
    state = {
        selectNumber: 0
    }

    //+
    increment = () => {
        let {selectNumber} = this.state;
        this.props.increment(selectNumber)
    }
    //-
    decrement = () => {
        let {selectNumber} = this.state;
        this.props.decrement(selectNumber)
    }
    incrementIfOdd = () => {
        let {selectNumber} = this.state;
        if (this.props.count % 2 !== 0) {
            this.props.increment(selectNumber)
        }
    }

    incrementAsync = () => {
        let {selectNumber} = this.state;
        this.props.incrementAsync(selectNumber, 1000)
    }

    //组件变换
    handleChange = (value) => {
        this.setState({
            selectNumber: value
        })
    }

    render() {
        return (
            <div>
                <h1>当前求和为:{this.props.count}</h1>
                <Space>
                    <Select style={{width: 300}}
                            onChange={this.handleChange}>
                        <Option value={1}>1</Option>
                        <Option value={2}>2</Option>
                        <Option value={3}>3</Option>
                    </Select>
                    <Button onClick={this.increment} type="primary">+</Button>
                    <Button onClick={this.decrement} type="primary">-</Button>
                    <Button onClick={this.incrementIfOdd} type="primary">奇数+</Button>
                    <Button onClick={this.incrementAsync} type="primary">异步加</Button>
                </Space>
            </div>
        );
    }
}

export default Count;